Custom Forecast Scripts
Python Forecast Template
The following example uses a Python script to demonstrate how to write custom forecasting logic in Discover. The following guides will explain how to use the script as a template for your own logic,
- The main function needs to be called "pyramid_forecast" and should have the following input arguments:
- dataLst – the list of the values that will drive the forecast
- futureValues – how much periods to project
- extraParams - any other inputs you wish to inject into the logic
The function returns a pandas "dataframe" with the a column for the forecast value(s) and optionally with 2 sets of intervals representing the prediction intervals of the forecast.
def pyramid_forecast(dataLst, period, futureValues, shouldCalcHistory, extraParams): outlierDetectionSensitivity = extraParams["OutlierDetectionSensitivity"] outlierSmoothingActualValueWeight = extraParams["OutlierDetectionSmoothingOriginalValueWeight"] # Add actual forecast logic here taking the period into consideration. # The forecast can optionally include historical perdictions in addition to the future values. forecast = [i for i in range(0, futureValues)] # Optional: # ---------- # Calculate confidence tval or anything else to create 1 or 2 interval lines. interval1High = [x+0.5 for x in forecast] interval1Low = [x - 0.5 for x in forecast] interval2High = [x + 1.5 for x in forecast] interval2Low = [x - 1.5 for x in forecast] # Create a pandas dataframe with the columns. # Note that the column names should be as they are in this template. # the 4 interval columns are optional. df = pandas.DataFrame({'forecast': forecast, 'interval1High': interval1High, 'interval1Low': interval1Low, 'interval2High': interval2High, 'interval2Low': interval2Low}) # Return the resulting dataframe in a dic return df;
R Forecast Template
The following example uses an R script to demonstrate how to write custom forecasting logic in Discover. It uses the same flow and concepts as the Python script above.
pyramid_forecast <- function(column, frequency, futures, shouldCalcHistory, extraParams) { outlierSensitivity = extraParams["OutlierDetectionSensitivity"] actualValueSmoothingWeight = extraParams["OutlierDetectionSmoothingOriginalValueWeight"] n<-futures # put the forecast results (optionally with historical predictions) forecast <- runif(n, min = 0, max = 100) # optional: first confidence interval intervalHigh1 <- runif(n, min = 0, max = 100) intervalLow1 <- runif(n, min = 0, max = 100) # optional: second confidence interval intervalHigh2 <- runif(n, min = 0, max = 100) intervalLow2 <- runif(n, min = 0, max = 100) # create the output dataframe with the prefixed column names. # if the intervals were not created, leave them out of the output dataframe. return( data.frame( 'forecast'=forecast, 'interval1High'=intervalHigh1, 'interval1Low'=intervalLow1, 'interval2High'=intervalHigh2, 'interval2Low'=intervalLow2)) }